﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ArchivesAdmin.Admin.Web.Controllers;
using ArchivesAdmin.Business.DataManage;
using ArchivesAdmin.Business.TrainingManage;
using ArchivesAdmin.Entity.DataManage;
using ArchivesAdmin.Entity.TrainingManage;
using ArchivesAdmin.Model.Param.TrainingManage;
using ArchivesAdmin.Util.Model;
using ArchivesAdmin.Web.Code;
using Microsoft.AspNetCore.Mvc;
using Oracle.ManagedDataAccess.Client;

namespace ArchivesAdmin.Admin.Web.Areas.DataManage.Controllers
{
    [Area("DataManage")]
    public class MigrationController : BaseController
    {
        private AjbxxbBLL ajbxxbBLL = new AjbxxbBLL();
        private static string orcl = "User Id=DHGX_DAGL; Password=123456; Data Source=localhost:1521/orcl;";

        #region 视图功能
        //[AuthorizeFilter("Training:teacher:view")]
        public IActionResult Index()
        {
            return View();
        }

        #endregion

        public static string SafeGetString(OracleDataReader reader, int colIndex)
        {
            if (!reader.IsDBNull(colIndex))
                return reader.GetString(colIndex);
            return string.Empty;
        }

        public static DateTime SafeGetDateTime(OracleDataReader reader, int colIndex)
        {
            if (!reader.IsDBNull(colIndex))
                return reader.GetDateTime(colIndex);
            return DateTime.MinValue;
        }

        public static int SafeGetInt32(OracleDataReader reader, int colIndex)
        {
            if (!reader.IsDBNull(colIndex))
                return reader.GetInt32(colIndex);
            return 0;
        }

        #region 提交数据
        private void ImportAAA()
        {

        }

        [HttpPost]
        [AuthorizeFilter("archive:archive:add")]
        public async Task<IActionResult> ImportData(int id)
        {  //  id: 用来识别是要导入什么档案     1:规划档案； 2:国土档案； 3：房屋档案； 4：文书档案； 5：物业、园区、招投标档案
           // 首先，要连接到Oracle数据库，读取dhda_ajbxxb表中数据，然后写入到 mysql 对应的 dhda_ajbxxb 表中

            TData<string> obj = new TData<string>();
            int rows = 0;

            using (OracleConnection conn = new OracleConnection(orcl))
            {
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    try
                    {
                        conn.Open(); 
                        cmd.CommandText = "select * from dhda_ajbxxb";

                        OracleDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            rows++;

                            AjbxxbEntity entity = new AjbxxbEntity();
                            entity.Id = 0;
                            entity.AUTOID = SafeGetInt32(reader, 0);
                            entity.QZH = SafeGetString(reader, 1);  
                            entity.MLH = SafeGetString(reader, 2);
                            entity.AJH = SafeGetString(reader, 3);
                            entity.FLH = SafeGetInt32(reader, 4);
                            entity.FLMC = SafeGetString(reader, 5);
                            entity.DAH = SafeGetString(reader, 6);
                            entity.AJM = SafeGetString(reader, 7);
                            entity.BCQX = SafeGetString(reader, 8);
                            entity.MJ = SafeGetString(reader, 9);
                            entity.QSRQ = SafeGetDateTime(reader, 10);
                            entity.JZRQ = SafeGetDateTime(reader, 11);
                            entity.CFWZ = SafeGetString(reader, 12);
                            entity.ZYS = SafeGetInt32(reader, 13);
                            entity.JS = SafeGetInt32(reader, 14);
                            entity.BZ = SafeGetString(reader, 15);
                            entity.CS = SafeGetInt32(reader, 16);

                            obj = await ajbxxbBLL.SaveForm(entity); 
                        }
                        obj.Result = rows.ToString();
                        obj.Tag = 1;
                        obj.Message = "档案数据迁移成功！共迁移数据行："   + obj.Result;

                        reader.Dispose();
                    }
                    catch (Exception ex)
                    {
                        obj.Result = rows.ToString();
                        obj.Tag = 0;
                        obj.Message = "档案数据迁移过程中产生异常：" +  ex.Message +  ", 发送异常的数据行号为："  +  obj.Result ;
                    }
                }
            }
           ; 

            return Json(obj);
        }

        #endregion

    }

}